package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.entity.charge.ChargeInvoice;
import org.jeecg.modules.charge.entity.*;
import org.jeecg.modules.charge.mapper.*;
import org.jeecg.modules.charge.service.IChargeInvoiceInhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 票据入库
 * @Author: jeecg-boot
 * @Date:   2019-08-08
 * @Version: V1.0
 */
@Service
@Transactional(readOnly = false)
public class ChargeInvoiceInhouseServiceImpl extends ServiceImpl<ChargeInvoiceInhouseMapper, ChargeInvoiceInhouse> implements IChargeInvoiceInhouseService {

    @Autowired
    private ChargeInvoiceInhouseMapper chargeInvoiceInhouseMapper;

    @Autowired
    private ChargeInvoiceTypeMapper chargeInvoiceTypeMapper;

    @Autowired
    private ChargeInvoiceMapper chargeInvoiceMapper;

    @Autowired
    private CancellactionMapper cancellactionMapper;

    @Autowired
    private ChargeInvoiceRecordsMapper chargeInvoiceRecordsMapper;

    @Override
    public String selectId(String id) {
        return chargeInvoiceInhouseMapper.selectId(id);
    }

    @Override
    public Integer saveba(ChargeInvoiceInhouse chargeInvoiceInhouse) {
        chargeInvoiceInhouseMapper.insert(chargeInvoiceInhouse);
        String startNum = chargeInvoiceInhouse.getStartNum();
        Integer length = startNum.length();
        //拼接起始号码
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append("0");
        }
        DecimalFormat df=new DecimalFormat(stringBuilder.toString());
        Integer results=0;
        Integer resultss = 0;
        Double money=0.0;

        ChargeInvoiceType chargeInvoiceType = chargeInvoiceTypeMapper.sureMoney(chargeInvoiceInhouse.getTypeNum());
        if (null !=chargeInvoiceType ){//不为空为定额发票
           if("1".equals(chargeInvoiceType.getIsFixed())){
               money = chargeInvoiceType.getMaxMoney();
           }
        }
        for (int i = Integer.valueOf(chargeInvoiceInhouse.getStartNum()); i <=Integer.valueOf(chargeInvoiceInhouse.getEndNum()) ; i++) {
            String hms = df.format(i);
            ChargeInvoice chargeInvoice = new ChargeInvoice();
            chargeInvoice.setMoney(money);
            chargeInvoice.setInvoiceHm(hms);
            chargeInvoice.setCreateDate(new Date());
            chargeInvoice.setInvoiceDm(chargeInvoiceInhouse.getGroupNo());
            Integer in = chargeInvoiceMapper.insert(chargeInvoice);
            resultss+=in;
        }
         results++;
        return resultss;
    }

    /**
     * 查询票据本编号是否已经生成
     *
     * @param chargeInvoiceInhouse
     * @return
     */
    @Override
    public Integer getGroupIsExits(ChargeInvoiceInhouse chargeInvoiceInhouse) {
        return chargeInvoiceInhouseMapper.getGroupIsExits(chargeInvoiceInhouse);
    }

    /**
     * 票据领用时修改票据状态
     *
     * @param chargeInvoiceInhouse
     * @return
     */
    @Override
    public void updateInHouse(ChargeInvoiceInhouse chargeInvoiceInhouse) {
        chargeInvoiceInhouseMapper.updateInHouse(chargeInvoiceInhouse);
    }

    @Override
    public List<String> getOfficeIdsByUid(String oid) {
        List<String>  oids=chargeInvoiceInhouseMapper.getOfficeIdsByUid(oid);
        //共享部门对应对应的领用部门
        if(null==oids){
            oids=new ArrayList<String>();
        }
        oids.add(oid);
        return oids;
    }

    @Override
    public List<ChargeInvoiceTree> getInvoicesByOids(List<String> oids, String typeNum) {
        List<ChargeInvoiceTree> invoices=chargeInvoiceInhouseMapper.getInvoicesByOids(oids,typeNum);
        return invoices;
    }

    @Override
    public String getOidByOcode(String orgCode) {
        String oid=chargeInvoiceInhouseMapper.getOidByOcode(orgCode);
        return oid;
    }

    @Override
    public List<ChargeInvoiceType> selectTypeAll() {
        return chargeInvoiceInhouseMapper.selectTypeAll();
    }

    @Override
    public List<ChargeInvoiceInhouse> findInvoicePage(IPage<ChargeInvoiceInhouse> page, ChargeInvoiceInhouse chargeInvoiceInhouse){
        return chargeInvoiceInhouseMapper.findInvoicePage(page,chargeInvoiceInhouse);
    }
    @Override
    public List<ChargeInvoiceInhouse> findInvoiceList(ChargeInvoiceInhouse chargeInvoiceInhouse){
        return chargeInvoiceInhouseMapper.findInvoiceList(chargeInvoiceInhouse);
    }
    @Override
    public Integer CancellactionHouse(Cancellaction cancellaction){
        Integer results =0;
        LoginUser loginUser = SsoUtil.getLoginUser();
        /**票据本数据核销*/
        ChargeInvoiceInhouse inhouse = new ChargeInvoiceInhouse();
        inhouse.setHouseIds(cancellaction.getHouseIdsList());
        inhouse.setCancelDate(cancellaction.getCanDate());
        inhouse.setCancelUser(loginUser.getRealname());
        inhouse.setUpdateBy(loginUser.getId());
        inhouse.setUpdateTime(new Date());
        /**票据信息表*/
        ChargeInvoice invoice =new ChargeInvoice();
        invoice.setCancelDate(cancellaction.getCanDate());
        invoice.setCancelUser(loginUser.getRealname());
        invoice.setInvoiceDms(cancellaction.getGroupNos());

        if(cancellaction != null){
            cancellaction.setCanName(loginUser.getRealname());
            cancellaction.setHouseIds(cancellaction.getHouseIdsList().toString());
            results = chargeInvoiceInhouseMapper.updateCanDate(inhouse);
            cancellactionMapper.insert(cancellaction);
            chargeInvoiceMapper.cancellScrap(invoice);
            if(cancellaction.getGroupNos().length > 0){
                chargeInvoiceRecordsMapper.updateCancell(cancellaction.getCanDate(),cancellaction.getGroupNos());
            }
        }
        return results;
    }

    /**一本票据入库*/
    public String insertInvoices(ChargeInvoiceInhouse chargeInvoiceInhouse){
        DecimalFormat df=new DecimalFormat("00000000");
        Integer results=0;
        Integer resultss = 0;
        Double money=0.0;
        Integer everyNum = Integer.valueOf(chargeInvoiceInhouse.getEveryNums());
        ChargeInvoiceType types = chargeInvoiceTypeMapper.selectByTypenum(chargeInvoiceInhouse.getTypeNum());
        if(null!=types) {
            //是定额发票入库
            if("1".equals(types.getIsFixed())) {
                money=types.getMaxMoney();
            }
        }
        if(chargeInvoiceInhouse.getIsInvoiceDM() != null && chargeInvoiceInhouse.getIsInvoiceDM().equals("true")){
				 Integer maxSernum = chargeInvoiceInhouseMapper.maxSerNum(); //最大流水号
				 chargeInvoiceInhouse.setSerNum(maxSernum+1);
				 String hm=df.format(maxSernum);
				 chargeInvoiceInhouse.setGroupNo(chargeInvoiceInhouse.getGroupNum() + DateUtils.getYear()+"-"+hm); //默认生成票据本编号年+流水号
				 for(int i=Integer.valueOf(maxSernum); i<Integer.valueOf(maxSernum)+chargeInvoiceInhouse.getEveryNums();i++) {
					 String hms=df.format(i);
					 ChargeInvoice  invoice =new ChargeInvoice();
					 invoice.setInvoiceHm(hms);
					 invoice.setMoney(money);
                     invoice.setCreateDate(new Date());
                     invoice.setCreateBy(SsoUtil.getLoginUser().getId());
					 invoice.setInvoiceDm(chargeInvoiceInhouse.getGroupNo());
                     invoice.setDelFlag("0");
                     Integer in = chargeInvoiceMapper.insert(invoice);
					 resultss+=in;
				 }
                 chargeInvoiceInhouse.setStartNum(df.format(Integer.valueOf(maxSernum)));
                 chargeInvoiceInhouse.setEndNum(df.format(Integer.valueOf(maxSernum)+chargeInvoiceInhouse.getEveryNums()-1));
                 money = money * everyNum;
                 BigDecimal moneyBig = new BigDecimal(money);
                 BigDecimal eyeryNumBig = new BigDecimal(everyNum);
                 chargeInvoiceInhouse.setMoney(moneyBig.multiply(eyeryNumBig).doubleValue());
                 chargeInvoiceInhouse.setTypeName(types.getName());
                 Integer isExits = chargeInvoiceInhouseMapper.getGroupIsExits(chargeInvoiceInhouse);//查询票据本编号是否已经存在
				 if(isExits <= 0) {
                     results = chargeInvoiceInhouseMapper.insert(chargeInvoiceInhouse);
				 }
        }else{
            Integer isExits = chargeInvoiceInhouseMapper.getGroupIsExits(chargeInvoiceInhouse);//查询票据本编号是否已经存在
            if(isExits > 0) {
                return  " 票据已生成！";
            }
            List<ChargeInvoice> list = new ArrayList();
            for(int i=Integer.valueOf(chargeInvoiceInhouse.getStartNum()); i<=Integer.valueOf(chargeInvoiceInhouse.getEndNum());i++) {
                String hms=df.format(i);
                ChargeInvoice  invoice =new ChargeInvoice();
                invoice.setId(String.valueOf(IdWorker.getId()));
                invoice.setMoney(money);
                invoice.setInvoiceHm(hms);
                invoice.setInvoiceDm(chargeInvoiceInhouse.getGroupNo());
                invoice.setCreateDate(new Date());
                invoice.setCreateBy(SsoUtil.getLoginUser().getId());
                list.add(invoice);
             }
            Integer in = 0;
            if(list.size() > 0){
                in = chargeInvoiceMapper.insertInvoiceList(list);
             }
            if(in > 0) {
                chargeInvoiceInhouse.setTypeName(types.getName());
                results = chargeInvoiceInhouseMapper.insert(chargeInvoiceInhouse);
            }
        }
        if(results > 0){
            return "生成票据成功！";
        }else{
            return "生成票据成功！";
        }
    }

}
